<?php

class Sistema_Form_Dicas extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_'.$controller);
        $this->setAttrib('class', 'formCtrl');
        
        $id = $front->getRequest()->getParam('id',0);
        if($id > 0){
            $data = new Sistema_Model_DbTable_Dicas();
            $data = $data->fetchRow('DIC_ID ='.$id);
        }
        $el = new Zend_Form_Element_Hidden('DIC_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        //$obj = new Sistema_Model_DbTable_Catsegmento();
        //$row = $obj->fetchAll()->toArray();
        //$arr = array();
        //foreach($row as $r) $arr[$r['CTS_ID']] = $r['CTS_NOM']; 
        
        //$el = new Zend_Form_Element_Select('CTS_ID',array('onchange' => ''));
        //$el->setLabel('Segmento')->setRequired(true)->addValidator('NotEmpty')->addMultiOptions($arr);
        //$this->addElement($el);
        
        $html = '';
        $obj = new Sistema_Model_DbTable_Catsegmento();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        $arr_set = array();
        if($id > 0 && strlen($data['CTS_ID']) == 1) $arr_set = array($data['CTS_ID']=>$data['CTS_ID']);
        else if($id > 0){
            $arr = explode(',',$data['CTS_ID']);
            foreach($arr as $a) $arr_set[$a] = $a;
        }
        
        $seg = new Sistema_Model_DbTable_Catsegmento;
        $seg = $seg->fetchAll(); 
        $html = '<select id="CTS_ID[]" name="CTS_ID[]" class="chosen-select" multiple="multiple" style="width:500px;">';
        foreach($seg as $s){
            $selected = array_search($s['CTS_ID'], $arr_set) > '' ? 'selected=""':'';            
            $html .= '<option '.$selected.' value="'.$s['CTS_ID'].'">'.$s['CTS_NOM'].'</option>'; 
        }
        $html .= '</select><br><br>';

        $el = new Zend_Form_Element('CTS_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Segmentos');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('DIC_NOM');
        $el->setLabel('Dica')
        ->setRequired(true) 
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
       
        $el = new Zend_Form_Element_Textarea('DIC_DES');
        $el->setLabel('Descrição')
        //->setRequired(true)
        //->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $obj = new Sistema_Model_DbTable_Categoriadicas();
        $row = $obj->fetchAll('CTD_STA = 1')->toArray();
        $arr = array();
        foreach($row as $r) $arr[$r['CTD_ID']] = $r['CTD_NOM']; 
        
        $el = new Zend_Form_Element_Select('CTD_ID',array('onchange' => ''));
        $el->setLabel('Categoria')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty')
        ->addMultiOptions($arr);
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('DIC_IMGLNK');
        $el->setLabel('LINK Imagem');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('DIC_URLLNK');
        $el->setLabel('Link Acesso');
        $this->addElement($el);
  
        
        $img = 'public/upload/'.$controller.'/'.$front->getRequest()->getParam('id').'.*';
        foreach (glob($img) as $src) {
            $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<img src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        
        $el = new Zend_Form_Element_File('DIC_FILE');
        $el->setLabel('Imagem - Tamanho: 290x115')
	->addValidator('Extension', false, array('jpg', 'png', 'gif'));
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('DIC_ORD');
        $el->setLabel('Ordem')
        ->setRequired(true)
        ->addFilter('int')
        ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('DIC_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('submit',array('disableLoadDefaultDecorators' =>true));
        $btn->setLabel('Gravar');
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-button btn-ok');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-button btn-cancel">Cancelar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-button btn-cancel');
        $this->addElement($btn);
        
    }


}



